import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* 布局Layout */
const appMain = r => require.ensure([], () => r(require('@/views/Main')), 'AppMain')
const appMain1 = r => require.ensure([], () => r(require('@/views/Main/index1.vue')), 'AppMain1')
const home = r => require.ensure([], () => r(require('@/views/Home')), 'home')
const header = r => require.ensure([], () => r(require('@/views/Main/Header')), 'header')
const rightContenc = r => require.ensure([], () => r(require('@/components/nav/rightContent.vue')), 'rightContenc')

// const clock = r => require.ensure([], () => r(require('@/components/common/time.vue')), 'clock')



const login = r => require.ensure([], () => r(require('@/views/Login')), 'login')
const error = r => require.ensure([], () => r(require('@/views/404')), 'error')

const xarticle = r => require.ensure([], () => r(require('@/views/XArticle')), 'xarticle')

const articleDetails = r => require.ensure([], () => r(require('@/views/XArticle/details.vue')), 'articleDetails')


export const constantRoutes = [
    {
        path: '/',
        type: 'appMain1',
        name: 'appMain1',
        component: appMain1,
        redirect: '/home',
        meta: { title: '首页', icon: 'home' },
        children: [{
            path: 'home',
            name: 'home',
            components: {
                default: home,
                top: header,
                aside:rightContenc
            },
            meta: { title: '首页', icon: 'home' },
            leaf: true, // 只有一个节点
            iconCls: 'iconfont icon-home', // 图标样式class
            menuShow: true
        },]

    },
    {
        path: '/xarticle',
        name: 'xarticle',
        component: appMain1,
        redirect: '/xarticle/list',
        children: [{
            path: '/xarticle/list',
            name: '/xarticle/list',
            components: {
                default: xarticle,
                top: header,
                aside:rightContenc
            },
            meta: { title: '首页', icon: 'home' },
            leaf: true, // 只有一个节点
            iconCls: 'iconfont icon-home', // 图标样式class
            menuShow: true
        },
        {
            path: '/xarticle/list',
            name: '/xarticle/list',
            components: {
                default: xarticle,
                top: header,
                aside:rightContenc
            },
            meta: { title: '列表', icon: 'home' },
            leaf: true, // 只有一个节点
            iconCls: 'iconfont icon-home', // 图标样式class
            menuShow: true
        },
        {
            path: '/details/:articleId',
            name: 'C#/Net',
            components: {
                default: articleDetails,
                top: header,
                aside:rightContenc
            },
            meta: { title: 'C#/Net', icon: 'home' },
            leaf: true, // 只有一个节点
            iconCls: 'iconfont icon-home', // 图标样式class
            menuShow: true
        },
        ]
    },
    /* 登录 */
    {
        path: '/login',
        component: login,
        meta: { title: '用户登录', icon: 'dashboard' },
        hidden: true
    },
    /* 报错页面404 */
    {
        path: '*',
        component: error,
        meta: { title: '404' },
        hidden: true
    }
]

const createRouter = () => new Router({
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    //base: '/bw/',
    routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
    const newRouter = createRouter()
    router.matcher = newRouter.matcher // reset router
}

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

export default router